/**
    ATClab - scriptable Air Traffic Control simulations.
    Copyright (C) 2010 openatclab@gmail.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
**/

#include "waypointsprite.h"
#include "canvas.h"
#include "logger.h"

using namespace atc;

///////////////////////////////////////////////////////////////////////////////
//
//
//
//-----------------------------------------------------------------------------
// Construction/Desturction
//

/*!
 * Constructor
 */
WayPointSprite::WayPointSprite(
	const std::string& name, QPoint pt, Canvas *canvas
)
	  : _point(0)
	  , _name(0)
	  , _visible(false)
{
	_point = new WPLocator(
			this, WAYPOINT_SPRITE_DIAMETER, WAYPOINT_SPRITE_DIAMETER, canvas
	);
	_point->setBrush( WAYPOINT_SPRITE_BRUSH );
	_point->setZ( WAYPOINT_SPRITE_LAYER );
	_point->move( pt.x(), pt.y() );

	QFontMetrics fm( WAYPOINT_SPRITE_FONT );

	_name = new WPText( name.c_str(), WAYPOINT_SPRITE_FONT, canvas );
	_name->setColor( WAYPOINT_SPRITE_COLOR );
	_name->setZ( WAYPOINT_SPRITE_LAYER );
	_name->move(pt.x(), pt.y() - fm.lineSpacing());

	toggle();
}

/*!
 * Destructor
 */
WayPointSprite::~WayPointSprite() {}

//-----------------------------------------------------------------------------
// [public] interface
//

/*!
 * Test for selection of waypoint.
 */
bool WayPointSprite::hit(QPoint pt)const{
	return  
		_name->boundingRect().contains(pt) ||
		_point->boundingRect().contains(pt);
}

/*!
 * Toggle visible status
 */
void WayPointSprite::toggle() {
	_visible = !_visible;
	_point->setVisible(_visible);
	_name->setVisible(_visible);
}

//-----------------------------------------------------------------------------
